package org.tpdata.api.singlequality.reviewdetect;

import asserts.Asserts;
import business.inhospitalqc.BatchCreate;
import business.inhospitalqc.CreateTemplate;
import business.inhospitalqc.CreateTemplateResult;
import business.inhospitalqc.TemplateResultList;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import com.google.common.base.Objects;
import common.CaseInfo;
import common.Data;
import common.RunCaseJson;
import common.SetUpTearDown;
import exceptions.TestNGRetry;
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.Contants;
import utils.ExcelUtils;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static common.TestStep.requestAndRespondBody;

@Feature("获取回顾质控评价生成监控检测图指标reportdatas数据")
public class GenMonChart   extends SetUpTearDown {
    Logger log = Logger.getLogger(GenMonChart.class);
    Data data = envSetUp();
CreateTemplateResult ctr=new CreateTemplateResult();
    CreateTemplate createTemplate=new CreateTemplate();
    TemplateResultList tr=new TemplateResultList();
    BatchCreate batchCreate=new BatchCreate();


    @Story("回顾质控评价生成监控检测图")
    @Test(retryAnalyzer = TestNGRetry.class,description = "回顾质控评价生成监控检测图", dataProvider = "datas")
    public void runCase(CaseInfo caseInfo) throws FileNotFoundException, ConfigurationException, YamlException {
      String templateId=  createTemplate.CreateModel(2);
        ctr.CreateModelResult(templateId);
        HashMap map =tr.GetPatientList(templateId,0);
        String batchID=batchCreate.BatchCreate37(1,map);

        JSONObject json = new JSONObject();
//        Map<String,String > json=new HashMap<String, String>();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("create_id", data.getUserId());
        json.put("reportNos", caseInfo.getReportNos());     //  报告号，多个值以逗号连接  值01002
        //nzkt0044 GOLD分级分布  GOLD分级I、II、III、IV四组的人次和占比
        //nzkt0045 急性加重次数分布
        //nzkt0046 入住ICU率
        //nzkt0047  氧疗使用率
        //nzkt0048  氧疗类型分布
        //nzkt0049 抗菌药物使用率
        //nzkt0050 支气管扩张剂使用率
        //nzkt0051 吸入糖皮质激素使用率
        //nzkt0052  全身糖皮质激素使用率
        //nzkt0053  合并症处理
        //nzkt0054 机械通气使用率
        //nzkt0055 无创通气使用率
        //nzkt0056 有创通气使用率
        //nzkt0057  有创通气模式分布
        //nzkt0058  入院前一年吸烟率
        //nzkt0059  入院前30天内吸烟状况分布图
        //nzkt0060  平均住院天数±标准差
        //nzkt0061  平均住院费用±标准差
        //nzkt0062  离院方式分布

        json.put("medCode", caseInfo.getMedCode());
        json.put("batch_id", batchID);
        log.info(json.toString());
        Response response = RunCaseJson.runCase(json, "get", caseInfo.getUri());
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);

        JSONObject obj = JSONObject.parseObject(jsonStr);
        JSONArray contentArr = obj.getJSONArray("responseData");
        String url = RestAssured.baseURI + ":" + RestAssured.port + caseInfo.getUri();
        requestAndRespondBody(url, json, jsonStr);
        boolean flag = Asserts.VerityValue(contentArr);
        Assert.assertEquals(flag, Contants.SUCCESS);
    }

 /*   public void runCase1() throws FileNotFoundException, ConfigurationException, YamlException {

        CreateTemplate createTemplate=new CreateTemplate();
        String td=createTemplate.CreateModel(0);
//        createTemplate.a();
        System.out.println(td);
//        Assert.assertNotEquals(td,1);

    }*/

    @DataProvider
    public Object[] datas() {
        List<CaseInfo> list = ExcelUtils.read(this.sheetIndex, 1, CaseInfo.class);
        List lt = new ArrayList();
        String StrName=this.getClass().getSimpleName();
        for (CaseInfo caseInfo : list) {
            if (Objects.equal(caseInfo.getClassName(),StrName) ) {
                lt.add(caseInfo);
            }
        }
        return lt.toArray();

    }

}